#include <iostream>
#include <vector>
#include <math.h>
#include <limits.h>
using namespace std;

void get_step(int i, vector<int>& a)
{
    for (int j = 2; j < sqrt(i); j++)
    {
        if (i % j == 0)
        {
            a.push_back(j);
            if (i / j != j)
                a.push_back(i / j);
        }
    }
}

int get_minstep(int n, int m)
{
    vector<int> step;
    step.resize(m, INT_MAX);
    step[n] = 0;
    for (int i = n; i < m; ++i)
    {
        if (step[i] == INT_MAX)
            continue;
        vector<int> a;
        get_step(i, a);

        for (int j = 0; j < a.size(); ++j)
        {
            if (i + a[j] < m && step[i + a[j]] != INT_MAX)
            {
                step[i + a[j]] = step[i + a[j]] < step[i] + 1 ? step[i + a[j]] : step[i] + 1;
            }
            else
            {
                step[i + a[j]] = step[i] + 1;
            }
        }
    }
    return step[m] == INT_MAX ? -1 : step[m];
}
int main()
{
    int n, m;
    int min_step = 0;
    while (cin >> n >> m)
    {
        min_step = get_minstep(n, m);
        cout << min_step << endl;
    }
    return 0;
}